package com.jxb.e20241230;

public class NextPermutation_31 {

    public static void main(String[] args) {

    }

    public void nextPermutation(int[] nums) {
        int i = nums.length - 2;
        //找到数组中的小数
        while (i >= 0 && nums[i] >= nums[i+1]) {
            i--;
        }
        //找到数组中尽可能小的大数，与小数替换
        if (i>0) {
            int j = nums.length - 1;
            while (j >=0 && nums[i] > nums[j]) {
                j--;
            }
            //交换小数与大数
            swap1(nums,i,j);
        }
        //将大数后面的所有数进行升序
        reverse1(nums,i+1);

    }

    private void reverse1(int[] nums, int i) {
        int left = i;
        int right = nums.length - 1;
        while (left < right) {
            swap1(nums,left,right);
            left++;
            right--;
        }
    }

    private void swap1(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

}
